package com.example.myapplication.view.RefactorView;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

import androidx.viewpager.widget.ViewPager;

import java.lang.reflect.Field;

public class MyViewPager extends ViewPager {
    private Context cxt;
    private int prov = 0;//上一次
    private LinearLayout pctr;
    private MyViewPager mvp = null;
    private boolean isTouch = false;
    private Handler hd = null;
    private FixedSpeedScroller scroller;
    private ItemChangeListener change;

    public MyViewPager(Context context) {
        super(context);
        initData(context);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData(context);
    }

    private void initData(Context context) {
        cxt = context;
        hd = new Handler();
        mvp = this;
        hd.post(new MyRunnable());
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        isTouch = true;
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            isTouch = false;
        }
        return super.onTouchEvent(ev);
    }

    public void setPagerDuration(ViewPager vp) {
        try {
            // 通过class文件获取mScroller属性
            Field mField = MyViewPager.class.getDeclaredField("scroller");
            mField.setAccessible(true);
            scroller = new FixedSpeedScroller(vp.getContext(), new AccelerateInterpolator());
            mField.set(vp, scroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
        scroller.setmDuration(3000);
    }

    @Override
    protected void onPageScrolled(int position, float offset, int offsetPixels) {
        super.onPageScrolled(position, offset, offsetPixels);
//        ((ImageView) pctr.getChildAt(prov)).setImageResource(R.drawable.white_point);
//        ((ImageView) pctr.getChildAt(position)).setImageResource(R.drawable.red_point);
//        prov = position;
        if (offset == 1) {
            if (change != null) {
                change.onChange(position);
            }
        }
    }

    private class MyRunnable implements Runnable {

        @Override
        public void run() {
            int pos = mvp.getCurrentItem();
            if (!isTouch) {
//                ImageView v = (ImageView) pctr.getChildAt(pos);
//                v.setImageResource(R.drawable.white_point);
//                prov = pos;
                if (pos == mvp.getAdapter().getCount() - 1) {
                    pos = -1;
                }
                mvp.setCurrentItem(++pos, true);
//                ImageView v1 = (ImageView) pctr.getChildAt(pos);
//                v1.setImageResource(R.drawable.red_point);
//                scroller.setmDuration(3500);
                if (change != null) {
                    change.onChange(pos);
                }
            }
            hd.postDelayed(this, 5000);
        }
    }

    public interface ItemChangeListener {
        void onChange(int pos);
    }

    ;

    private class FixedSpeedScroller extends Scroller {
        private int mDuration = 3000;

        public FixedSpeedScroller(Context context) {
            super(context);
        }

        public FixedSpeedScroller(Context context, Interpolator interpolator) {
            super(context, interpolator);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, mDuration);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy) {
            super.startScroll(startX, startY, dx, dy, mDuration);
        }

        /**
         * 设置切换时间
         */
        public void setmDuration(int time) {
            mDuration = time;
        }

        /**
         * 获取切换时间
         */
        public int getmDuration() {
            return mDuration;
        }

    }

}

